package frame.project;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.droid4you.util.cropimage.CropImage;

public class Edit extends Activity {
	Button editBack, editCrop, editRotate, editBlur, editDone;
	Button effectNone, effectGreyscale, effectInvert, effectSepia,
			effectBright, effectDark, effectTint;
	ImageProcess imageProcess = new ImageProcess();
	ImageProduce image = new ImageProduce();
	DBAdapter db = new DBAdapter(this);
	Handler mHandler = new Handler();
	Bitmap bmEdit, bmOri;
	ImageView imageView;
	int id = 0, blurValue = 1, request_code = 0, request_crop = 1;
	String path;
	Boolean cond = false;
	private HorizontalScrollView horViewImagesFilter;
	private LinearLayout panelSeekBar;
	SeekBar blurBar;

	/**
	 * Opens AlbumGrid class to choose which image to edit
	 * 
	 * @author Marcel
	 * 
	 */
	public void chooseImage() {
		Intent intent = new Intent(this, AlbumGrid.class);
		intent.putExtra("color", "Choose");
		startActivityForResult(intent, request_code);
	}

	public void saveImage() {

		File file = new File(path);
		OutputStream outStream = null;
		try {
			outStream = new FileOutputStream(file);
			bmEdit.compress(Bitmap.CompressFormat.PNG, 100, outStream);
			outStream.flush();
			outStream.close();

			Toast.makeText(this, "Saved", Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
		db.open();
		db.updateDate(Long.toString(file.lastModified()), id);
		db.close();

		finish();
	}

	/**
	 * Check if there is a value passed from the previous activity, which is
	 * calling AlbumGrid class Value passed from Album Grid class is a image's
	 * path string
	 * 
	 * @author Marcel
	 * 
	 */
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == request_code) {
				path = data.getStringExtra("path");
				id = Integer.parseInt(data.getStringExtra("id"));

				// Decode image based on the path received
				bmEdit = image.decodeBitmap(path, 400, 400);
				bmOri = bmEdit;

				// Set imageView with the image taken from the path
				imageView.setImageBitmap(bmEdit);
			}

			if (requestCode == request_crop) {

				bmEdit = data.getExtras().getParcelable("image");
				bmOri = bmEdit;

				imageView.setImageBitmap(bmEdit);
			}
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit);

		// Create an image with bitmap
		imageView = (ImageView) findViewById(R.id.imageView1);
		imageView.setScaleType(ScaleType.FIT_CENTER);

		try {
			// Check if there is a path passed from camera activity
			Bundle extras = getIntent().getExtras();
			path = extras.getString("strValue").toString();
			id = extras.getInt("id");
			bmEdit = image.decodeBitmap(path, 400, 400);
			bmOri = bmEdit;
			imageView.setImageBitmap(bmEdit);
		} catch (Exception e) {
			// If there is nothing passed
			chooseImage();
		}

		// Linear layout for the seek bar of blur method
		panelSeekBar = (LinearLayout) findViewById(R.id.panelSeekBar);

		// Seek bar for the blur method
		blurBar = (SeekBar) findViewById(R.id.blurBar);

		// Horizontal view for the images filter
		horViewImagesFilter = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);

		// Button for the Back, Crop, Rotate, Blur, and Done
		editBack = (Button) findViewById(R.id.editBack);
		editCrop = (Button) findViewById(R.id.editCrop);
		editRotate = (Button) findViewById(R.id.editRotate);
		editBlur = (Button) findViewById(R.id.editBlur);
		editDone = (Button) findViewById(R.id.editDone);

		// Button for effect filter
		effectNone = (Button) findViewById(R.id.effectNone);
		effectGreyscale = (Button) findViewById(R.id.effectGreyscale);
		effectInvert = (Button) findViewById(R.id.effectInvert);
		effectSepia = (Button) findViewById(R.id.effectSepia);
		effectBright = (Button) findViewById(R.id.effectBrightness);
		effectDark = (Button) findViewById(R.id.effectDarker);
		effectTint = (Button) findViewById(R.id.effectTint);

		// SetOnClickListener for back button
		editBack.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

		// SetOnClickListener for crop button
		editCrop.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						CropImage.class);
				Bundle extras = new Bundle();
				extras.putString("path", path);
				intent.putExtras(extras);
				startActivityForResult(intent, request_crop);

			}
		});

		// SetOnClickListener for rotate button
		editRotate.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bmEdit = imageProcess.performRotate(bmEdit);
				bmOri = bmEdit;
				imageView.setImageBitmap(bmEdit);
			}
		});

		// SetOnClickListener for blur button
		editBlur.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// If condition to change the current horizontal view of filter
				// effect to the seek bar of blur method and conversely
				if (cond == true) {
					horViewImagesFilter.setVisibility(View.VISIBLE);
					panelSeekBar.setVisibility(View.GONE);
					cond = false;
				} else {
					panelSeekBar.setVisibility(View.VISIBLE);
					horViewImagesFilter.setVisibility(View.GONE);
					cond = true;
				}

				blurBar.setOnSeekBarChangeListener(blurBarChangeListener);
				blurBar.setProgress(0);
			}
		});

		// SetOnClickListener for image filter "None" button
		effectNone.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				imageView.setImageBitmap(bmOri);
			}
		});

		// SetOnClickListener for image filter "Grey-Scale" button
		effectGreyscale.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bmEdit = imageProcess.doGreyscale(bmOri);
				imageView.setImageBitmap(bmEdit);
			}
		});

		// SetOnClickListener for image filter "Invert" button
		effectInvert.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bmEdit = imageProcess.doInvert(bmOri);
				imageView.setImageBitmap(bmEdit);
			}
		});

		// SetOnClickListener for image filter "Sepia" button
		effectSepia.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bmEdit = imageProcess.doSepia(bmOri, 40, 1.5, 0.6, 0.12);
				imageView.setImageBitmap(bmEdit);
			}
		});

		// SetOnClickListener for image filter "Bright" button
		effectBright.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bmEdit = imageProcess.doBright(bmOri, 100);
				imageView.setImageBitmap(bmEdit);
			}
		});

		// SetOnClickListener for image filter "Dark" button
		effectDark.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bmEdit = imageProcess.doDark(bmOri, 0.6, 0.6, 0.6);
				imageView.setImageBitmap(bmEdit);
			}
		});

		// SetOnClickListener for image filter "Tint" button
		effectTint.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				bmEdit = imageProcess.doTint(bmOri, 50);
				imageView.setImageBitmap(bmEdit);
			}
		});

		// SetOnClickListener for done button
		editDone.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				saveImage();
			}
		});

	}

	// OnSeekBarListener for blur method
	OnSeekBarChangeListener blurBarChangeListener = new OnSeekBarChangeListener() {
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			blurValue = progress + 1;
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			bmEdit = imageProcess.performBlur(bmOri, blurValue);
			bmOri = bmEdit;
			imageView.setImageBitmap(bmEdit);
		}
	};
}